<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_href} function plugin
 *
 * File:       function.pp_href.php<br>
 * Type:       function<br>
 * Name:       pp_href<br>
 * Date:       24.Feb.2003<br>
 * Purpose:    Prints out a list of text input types<br>
 * Input:<br>
 *           - name       (optional) - string default "text"
 *           - values     (required) - array
 *           - options    (optional) - associative array
 *           - checked    (optional) - array default not set
 *           - separator  (optional) - ie <br> or &nbsp;
 *           - output     (optional) - the output next to each text button
 *           - assign     (optional) - assign the output as an array to this variable
 * Examples:
 * <pre>
 * {pp_href values=$ids output=$names}
 * {pp_href values=$ids name='box' separator='<br>' output=$names}
 * {pp_href values=$ids checked=$checked separator='<br>' output=$names}
 * </pre>
 * @link http://smarty.php.net/manual/en/language.function.html.texts.php {pp_href}
 *      (Smarty online manual)
 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
 * @author credits to Monte Ohrt <monte at ohrt dot com>
 * @version    1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_href($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');

	$name = NULL;
	$text = NULL;
	$except = NULL;
	$value = NULL;
	$extra = NULL;
	
	$except = NULL;
	
	foreach($params as $_key => $_val) {
		switch($_key) {
			case 'name':
				$$_key = (string)$_val;
				break;
			
			case 'text':
				$$_key = (string)$_val;
				break;
			
			case 'except':
				$$_key = (string)$_val;
				break;

			case 'value':
				$$_key = (string)$_val;
				break;
			
			case 'assign':
				break;
			
			default:
				if( !is_array($_val) ) {
					$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
				} else {
					$smarty->trigger_error("pp_href: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
				}
				break;
		}
	}

	//
	$_html_result = smarty_function_pp_href_output($name, $text, $except, $value, $extra);

	return $_html_result;
}

function smarty_function_pp_href_output($name, $text, $except, $value, $extra) {
	$_output = '';
	
	$_link = '';
	if( isset($except) && isset($value) ) {
		if( isset($_GET) && is_array($_GET) ) {
			if( isset($_GET[$except]) ) {
				$_link .= $_SERVER['REDIRECT_URL'];
				$_link .= '?';
				foreach($_GET as $k => $v) {
					if( $k == $except ) {
						$_link .= $k . '=' . $value;
					} else {
						$_link .= $k . '=' . $v;
					}
					$_link .= '&';
				}
				if( substr($_link, -1) == '&' ) {
					$_link = substr($_link, 0, -1);
				}
			} else {
				$_link = $_SERVER['REQUEST_URI'];
				if( strpos($_link, '?') === false ) {
					$_link .= '?' . $except . '=' . $value;
				} else {
					$_link .= '&' . $except . '=' . $value;
				}
			}
		} else {
			$_link .= $_SERVER['REDIRECT_URL'];
			$_link .= '?';
			$_link .= $except . '=' . $value;
		}
	} else {
		$_link = '#';
	}
	$_output .= '<a href="' . $_link . '" ' . $extra . '>' . $text . '</a>';
	
	return $_output;
}
//
?>